/*-
 * Copyright 2012 Guram Dukashvili
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
//---------------------------------------------------------------------------
#ifndef __vector_h__
#define __vector_h__
//-----------------------------------------------------------------------------
#include "config.h"
#include "array.h"
//---------------------------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
//---------------------------------------------------------------------------
typedef struct vector {
  array pointers;
  uint8_t pointers_static_data[sizeof(void *) * 8];

  uintptr_t element_size;

  union {
    void * element_new;
    void * (* element_newf)(void * object);
  };
  union {
    void * element_delete;
    void (* element_deletef)(void * data,void * object);
  };
  union {
    void * element_copy;
    void (* element_copyf)(void * dst,const void * src,void * object);
  };
  union {
    void * element_compare;
    intptr_t (* element_comparef)(const void * data,const void * data_in_vector,void * object);
  };

} vector;
//---------------------------------------------------------------------------
vector * vector_new();
void vector_init(vector * v);
void vector_destroy(vector * v);
void vector_delete(vector * v);
void vector_copy(vector * v,const vector * src);
void vector_resize(vector * v,uintptr_t n);
//---------------------------------------------------------------------------
void vector_append(vector * v);
void vector_append_ptr(vector * v,const void * data);
void vector_insert(vector * v,uintptr_t idx);
void vector_insert_ptr(vector * v,uintptr_t idx,const void * data);
void vector_remove(vector * v,uintptr_t idx);
void vector_xchg_elements(vector * v,uintptr_t idx,uintptr_t new_idx);
void vector_copy_paste(
  vector * v,
  uintptr_t dst_position,
  const vector * src,
  uintptr_t src_position,
  uintptr_t count);
//---------------------------------------------------------------------------
intptr_t vector_lookup(const vector * v,const void * data,uintptr_t * insert_position);
intptr_t vector_lookup_ptr(const vector * v,const void * ptr);
void vector_sort(vector * v);
//---------------------------------------------------------------------------
static __inline__ void * vector_get(vector * v,uintptr_t idx)
{
#if COMPILE_RUNTIME_CHECKS
  if( idx >= v->pointers.count ){
    fprintf(stderr,"undefined behavior in %s, line %d\n",__FILE__,__LINE__);
    abort();
  }
#endif
  return *((void **) v->pointers.a + idx);
}
//---------------------------------------------------------------------------
static __inline__ void * vector_append_get(vector * v)
{
  uintptr_t idx = v->pointers.count;
  vector_resize(v,idx + 1);
#if COMPILE_RUNTIME_CHECKS
  if( idx >= v->pointers.count ){
    fprintf(stderr,"undefined behavior in %s, line %d\n",__FILE__,__LINE__);
    abort();
  }
#endif
  return *((void **) v->pointers.a + idx);
}
//---------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif
//---------------------------------------------------------------------------
#endif
//-----------------------------------------------------------------------------
